/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package beans;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;

/**
 *
 * @author Muse
 */
@ManagedBean(name = "ratingBean")
@RequestScoped
public class RatingBean {

    private Integer rating;
    private Integer average;
    private Integer code;
    private String restaurant;
    private String comment;
    private final logica.StructureManager manager;
    private final logica.RatingDriver driver;
    private final java.util.ArrayList<String> restaurants;
    private java.util.ArrayList<logica.ItemRating> ratings;    
    
    /**
     * Creates a new instance of RatingBean
     */
    public RatingBean() {
        rating = 0;
        code = 1;
        comment = "";
        driver = new logica.RatingDriver();
        manager = driver.getRestaurants();
        restaurants = manager.getRestaurantNames();
        restaurant = restaurants.get(0);
        driver.obtainRatings(manager);
        ratings = manager.getRatingsFromRestaurant(0);
        average = manager.getAverageFromRestaurant(0);        
    }
    public void handleCancel() {
        //Add facesmessage
    }    
    
    public void onRestaurantChange() {
        rating = 0;
        comment = "";
        ratings = manager.getRatingsFromRestaurant(restaurant);
        average = manager.getAverageFromRestaurant(restaurant);
    }
    
    public void annotate() {
        //here the magic
        ratings = manager.getRatingsFromRestaurant(restaurant);
        int tmpcode = manager.getIdRestaurant(restaurant);
        manager.addRatingToAverageRestaurant(tmpcode-1, rating);
        average = manager.getAverageFromRestaurant(tmpcode-1);
        logica.ItemRating newitem = new logica.ItemRating();
        newitem.setComment(comment);
        newitem.setScore(rating);
        newitem.setResCode(tmpcode);
        newitem.setUsrCode(1);
        ratings.add(0, newitem);
        driver.putCommentOnDB(comment, rating, tmpcode, 1);
        
        rating = 0;
        comment = "";
    }
        
    //my getters and setters
    
    public void setRestaurant(String rest) {
        restaurant = rest;
    }
    
    public void setComment(String comentario) {
        comment = comentario;
    }

    public void setRating(Integer rat){
        rating = rat;
    }
    
    public void setAverage(Integer promedio) {
        average = promedio;
    }
    
    public void setCode(Integer codigo){
        code = codigo;
        restaurant = restaurants.get(codigo-1);
        ratings = manager.getRatingsFromRestaurant(restaurant);
        average = manager.getAverageFromRestaurant(restaurant);
    }
    
    public java.util.ArrayList<String> getRestaurants(){
        return restaurants;
    }    
    
    public java.util.ArrayList<logica.ItemRating> getRatings(){
        return ratings;
    }
    
    public String getRestaurant() {
        return restaurant;
    } 
    
    public String getComment() {
        return comment;
    }

    public Integer getRating(){
        return rating;
    }    
    
    public Integer getAverage() {
        return average;
    }
    
    public Integer getCode() {
        return code;
    }    
}
